package com.zby.business.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.zby.business.R;
import com.zby.business.adapter.MyFragmentPagerAdapter;
import com.zby.business.fragment.MarketFragment;
import com.zby.business.fragment.MineFragment;
import com.zby.business.fragment.OnSaleFragment;
import com.zby.business.fragment.ShoppingCarFragment;
import com.zby.business.fragment.ShouYeFragment;
import com.zby.business.util.App;
import com.zby.business.util.Util;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {
    private LinearLayout gouwuche, shouye, tejia, shezhi;

    private FrameLayout mainFram;
    private ViewPager viewPager;
    private FragmentManager Fm;
    private FragmentTransaction Ft;
    private ShoppingCarFragment shoppingCarFragment;
    private OnSaleFragment onSaleFragment;
    private ShouYeFragment shouYeFragment;
    private MineFragment mineFragment;
    private MarketFragment marketFragment;
    private ArrayList<Fragment> fragments;
    private ArrayList<LinearLayout> layouts;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);
        initView();
        App app = (App) getApplication();
        SharedPreferences sharedPreferences = getSharedPreferences("business", Activity.MODE_PRIVATE);
        String name = sharedPreferences.getString("username", "");
        String userId = sharedPreferences.getString("userId", "");
        float account = sharedPreferences.getFloat("account", 0);
        if (!name.equals("") && !userId.equals("")) {
            app.setName(name);
            app.setUserId(userId);
            app.setAccount(account);
        }
        MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(myFragmentPagerAdapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < 4; i++) {
                    if (i == position) {
                        layouts.get(i).setBackgroundResource(R.color.orange);
                    } else {
                        layouts.get(i).setBackgroundResource(R.color.lightblue);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    private void initView() {

        Fm = getSupportFragmentManager();
        Ft = Fm.beginTransaction();
        fragments = new ArrayList<Fragment>();
        layouts = new ArrayList<LinearLayout>();
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        shouye = (LinearLayout) findViewById(R.id.shouye);
        shezhi = (LinearLayout) findViewById(R.id.shezhi);
        gouwuche = (LinearLayout) findViewById(R.id.gouwuche);
        tejia = (LinearLayout) findViewById(R.id.tejia);
        layouts.add(shouye);
        layouts.add(tejia);
        layouts.add(gouwuche);
        layouts.add(shezhi);
        mainFram = (FrameLayout) findViewById(R.id.mainFrame);
        shouye.setOnClickListener(this);
        shezhi.setOnClickListener(this);
        gouwuche.setOnClickListener(this);
        tejia.setOnClickListener(this);
        shoppingCarFragment = new ShoppingCarFragment();
        onSaleFragment = new OnSaleFragment();
        shouYeFragment = new ShouYeFragment();
        mineFragment = new MineFragment();
        marketFragment = new MarketFragment();
        fragments.add(shouYeFragment);
        fragments.add(marketFragment);
        fragments.add(shoppingCarFragment);
        fragments.add(mineFragment);
//        Ft.add(R.id.mainFrame, searchFragment, "SEARCHFRAGMENT");
    }


    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            System.out.println("nav_camera");
            Toast.makeText(MainActivity.this, "camera", Toast.LENGTH_LONG).show();
        } else if (id == R.id.nav_gallery) {
            System.out.println("nav_gallery");
            Toast.makeText(MainActivity.this, "gallery", Toast.LENGTH_LONG).show();
        } else if (id == R.id.nav_slideshow) {
            System.out.println("nav_slideshow");
        } else if (id == R.id.nav_manage) {

            System.out.println("nav_manage");
        } else if (id == R.id.nav_share) {
            System.out.println("nav_share");
        } else if (id == R.id.nav_send) {
            intent = new Intent(getApplicationContext(), Login.class);
            startActivity(intent);
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onClick(View v) {
        Ft = Fm.beginTransaction();
        switch (v.getId()) {
            case R.id.shouye:
                viewPager.setCurrentItem(0);
                break;
            case R.id.tejia:
                viewPager.setCurrentItem(1);
                break;
            case R.id.gouwuche:
                viewPager.setCurrentItem(2);
                break;
            case R.id.shezhi:
                viewPager.setCurrentItem(3);
                break;
        }
        setBackground(v.getId());
        Ft.commit();
    }

    private long time;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (Fm.getBackStackEntryCount() > 0) {
                Fm.popBackStack();
            } else {
                long currentTime = System.currentTimeMillis();
                if (currentTime - time < 1000) {
                    finish();
                } else {
                    time = currentTime;
                    Util.toast(getApplicationContext(), "再次点击退出程序");
                }
            }
        }
        return true;
    }

    private void setBackground(int id) {
        for (LinearLayout layout : layouts) {
            if (layout.getId() == id) {
                layout.setBackgroundResource(R.color.orange);
            } else {
                layout.setBackgroundResource(R.color.lightblue);
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}
